'''
Created on 27. okt. 2010

@author: kukovec
'''

import wx
import sys
import backends.googlemaps


class mapScrolledWindow(wx.ScrolledWindow):
    def __init__(self, aParent):
        wx.ScrolledWindow.__init__(self, aParent)
        self.Bind(wx.EVT_PAINT, self.onPain, self)
        self.Bind(wx.EVT_SIZE, self.onSize, self)
        self.Bind(wx.EVT_LEFT_DOWN, self.onMouseLeftDown, self)
        self.Bind(wx.EVT_LEFT_UP, self.onMouseLeftUp, self)
        self.Bind(wx.EVT_LEAVE_WINDOW, self.onMouseLeaveWindow, self)
        self.Bind(wx.EVT_MOTION, self.onMouseMotion, self)
        self.Bind(wx.EVT_MOUSEWHEEL, self.onMouseWheel, self)
   
        self.longitudePosition = 0.0
        self.latitudePosition = 0.0
        self.mouseMovePoint = wx.Point(0, 0)
        self.mouseDown = False
        self.focusPoint = [0, 0]
        
        # Test
        self.mapBitmap = None
        self.backend = backends.googlemaps.CGoogleMapsBackend(self.refreshMapData)
        self.backend.getData(wx.Size(self.GetParent().GetClientSize().GetWidth(), self.GetParent().GetClientSize().GetHeight()))
    def refreshMapData(self, aMapBitmap, aFocusPoint):
        self.mapBitmap = aMapBitmap
        self.focusPoint = aFocusPoint
        self.Refresh()
    def onSize(self, aEvent):
        self.backend.getData(wx.Size(self.GetParent().GetClientSize().GetWidth(), self.GetParent().GetClientSize().GetHeight()))
        return
        self.adjustScrollbars()
        #print 'onSize --> width: %d, height: %d' % (aEvent.GetSize().GetWidth(), aEvent.GetSize().GetHeight())
        if aEvent.GetSize().GetWidth() > self.maps[self.zoomLevel].GetWidth() or aEvent.GetSize().GetHeight() > self.maps[self.zoomLevel].GetHeight():
            #print 'onSize correction'
            correctedSize = aEvent.GetSize()
            if aEvent.GetSize().GetWidth() > self.maps[self.zoomLevel].GetWidth():
                correctedSize.SetWidth(self.maps[self.zoomLevel].GetWidth())
            if aEvent.GetSize().GetHeight() > self.maps[self.zoomLevel].GetHeight():
                correctedSize.SetHeight(self.maps[self.zoomLevel].GetHeight())
            self.GetParent().SetClientSize(correctedSize)
        else:
            self.adjustScrollbars()
    def onPain(self, aEvent):
        #print 'onPaint'
        dc = wx.PaintDC(self)
        if None != self.mapBitmap:
            #self.DoPrepareDC(dc)
            #wx.BufferedDC(dc, self.mapBitmap)
            dc.DrawBitmap(self.mapBitmap, self.GetParent().GetClientSize().GetWidth() / 2 - self.mapBitmap.GetWidth() / 2, self.GetParent().GetClientSize().GetHeight() / 2 - self.mapBitmap.GetHeight() / 2)      
    def onMouseMotion(self, aEvent):
        return
        if True == self.mouseDown:            
            if self.mouseMovePoint.x == 0 and self.mouseMovePoint.y == 0:
                self.mouseMovePoint = aEvent.GetPosition()
            else:
                if self.mouseMovePoint.x != aEvent.GetPosition().x or self.mouseMovePoint.y != aEvent.GetPosition().y:
                    self.Scroll(self.GetViewStart()[0] - (aEvent.GetPosition().x - self.mouseMovePoint.x), self.GetViewStart()[1] - (aEvent.GetPosition().y - self.mouseMovePoint.y))
                    self.mouseMovePoint = aEvent.GetPosition()             
    def onMouseLeftDown(self, aEvent):
        return
        self.mouseDown = True
        self.mouseMovePoint = aEvent.GetPosition()
        self.calculateMapPosition()
        self.refreshTitle()
    def onMouseLeftUp(self, aEvent):
        return
        #print 'onMouseLeftUp'
        self.mouseDown = False
        self.mouseMovePoint = wx.Point(0, 0)
    def onMouseLeaveWindow(self, aEvent):
        return
        #print 'onMouseLeaveWindow'
        self.mouseDown = False
        self.mouseMovePoint = wx.Point(0, 0)
    def adjustScrollbars(self):
        return
        bitmapXAxis = self.longitudePosition * self.maps[self.zoomLevel].GetWidth() / 360.0 - self.mouseMovePoint.x
        bitmapYAxis = self.latitudePosition * self.maps[self.zoomLevel].GetHeight() / 180.0 - self.mouseMovePoint.y
        #print 'adjustScrollbars --> x: %d, y: %d' % (bitmapXAxis, bitmapYAxis)
        self.SetScrollbars(1, 1, self.maps[self.zoomLevel].GetWidth(), self.maps[self.zoomLevel].GetHeight(), bitmapXAxis, bitmapYAxis)
        self.refreshTitle()
    def onMouseWheel(self, aEvent):
        #print 'onMouseWheel --> x: %d, y: %d' % (aEvent.GetPosition().x, aEvent.GetPosition().y)
        #self.mouseMovePoint = aEvent.GetPosition()
        if aEvent.GetWheelRotation() > 0:
            self.backend.increaseZoomLevel()
            self.backend.getData(wx.Size(self.GetParent().GetClientSize().GetWidth(), self.GetParent().GetClientSize().GetHeight()))
        elif aEvent.GetWheelRotation() < 0:
            self.backend.decreaseZoomLevel()
            self.backend.getData(wx.Size(self.GetParent().GetClientSize().GetWidth(), self.GetParent().GetClientSize().GetHeight()))
    def calculateMapPosition(self):
        return
        self.longitudePosition = (self.GetViewStart()[0] + self.mouseMovePoint.x) * 360.0 / self.maps[self.zoomLevel].GetWidth()
        self.latitudePosition = (self.GetViewStart()[1] + self.mouseMovePoint.y) * 180.0 / self.maps[self.zoomLevel].GetHeight()
        #self.longitudePosition = aMousePosition.GetPosition().x * 360 / self.maps[self.zoomLevel].GetWidth()
        #self.latitudePosition = aMousePosition.GetPosition().y * 180 / self.maps[self.zoomLevel].GetHeight()        
        #print 'Longitude: %d' % (self.longitudePosition)
        #print 'Latitude: %d' % (self.latitudePosition)
    def refreshTitle(self):
        return
        self.GetParent().SetTitle(self.GetParent().title + ' - ' + 'Zoom: ' + str(self.zoomLevel) + ', Longitude: ' + str(self.longitudePosition) + ', Latitude: ' + str(self.latitudePosition))
        
        
class mainFrame(wx.Frame):
    def __init__(self):
        #wx.Frame.__init__(self, None, wx.ID_ANY, 'Google Maps View', wx.DefaultPosition, wx.DefaultSize, wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
        wx.Frame.__init__(self, None, wx.ID_ANY, '', size = wx.Size(768, 768))
        self.title = 'Map World'
        self.SetTitle(self.title)
        self.Bind(wx.EVT_CLOSE, self.onAppClose, self)
        self.scrolledWindow = mapScrolledWindow(self)
    def onAppClose(self, aEvent):
        #self.scrolledWindow.mapBitmap.SaveFile('world.png', wx.BITMAP_TYPE_PNG)
        self.Destroy()
        sys.exit()

class mainApp(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        frame = mainFrame()
        frame.Centre()
        frame.Show(True)
        self.SetTopWindow(frame)
        return True
    
if __name__ == '__main__':
    app = mainApp(False)
    app.MainLoop()